<?php
/**
 * 阿里云基础库
 *
 * @author     yyxi<yyxi@vip.deyi.com>
 * @since      2017/9/28 11:04
 * @copyright  deyi.com
 */
namespace Deyi\Functions;

/**
 * Class AliyunFunction
 * @package Deyi\Functions
 */
class AliyunFunction
{
    /**
     * 生成鉴权id
     * @param $url
     * @param $privateKey
     * @param int $time 失效时间
     * @return string
     */
    public static function getAuthKey($url, $privateKey, $time = 1800)
    {
        $time = time() + $time;

        $hashValue = md5('/' . $url . '-' . $time . '-0-0-' . $privateKey);

        return $time . '-0-0-' . $hashValue;
    }

    /**
     * url编码
     * @param $str
     * @return mixed|string
     */
    public static function percentEncode($str)
    {
        $res = urlencode($str);
        $res = preg_replace('/\+/', '%20', $res);
        $res = preg_replace('/\*/', '%2A', $res);
        $res = preg_replace('/%7E/', '~', $res);
        return $res;
    }

    /**
     * 对待加密字符串加密
     * @param $source
     * @param $accessSecret
     * @return string
     */
    public static function signString($source, $accessSecret)
    {
        return base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
    }
}
